/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.pages.class.msg')
        .controller('MsgListCtrl', MsgListCtrl);

    function MsgListCtrl($scope,AppUtils,MsgService) {

        var vm = this;
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.pageIndex = 0;

        $scope.loadMoreMsg = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                vm.getMsgPage();
            }
        }

        vm.getMsgPage = function(){
            var params = {};
            params.pageInfo = {};
            params.pageInfo.pageSize = 10;
            params.pageInfo.pageNumber = vm.pageIndex;

            MsgService.loadMsgPage(params).then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStatus = false;
                    }
                    angular.forEach(items,function(data){
                        vm.exhortaions.push(data);
                    })
                }
            }).finally(function() {
                // Stop the ion-refresher from spinning
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        }
    }
})();
